import bb.cascades 1.3
import bb.WebImageView 1.4
import bb.data 1.0
import "lib"
import "controls"

NavigationPane {
    id: root
    property int pageIndex: 1
    property string baseUrl
    signal navRootChanged(variant root)
    
    onBaseUrlChanged: {
        dataSource.source = baseUrl;
        _Setting.RequestReg();
    }
    
    function refresh() {
        root.pageIndex = 1;
        var tmpUrl = baseUrl.replace(/&page=\d{0,}/, "&page=" + root.pageIndex);
        tmpUrl = tmpUrl.replace(/&random=\d{0,}/, "&random=" + (Math.random() * 100000).toFixed(0));
        dataSource.source = tmpUrl
    }
    
    function loadMore() {
        root.pageIndex = parseInt(root.pageIndex) + 1;
        dataSource.source = baseUrl.replace(/&page=\d{0,}/, "&page=" + root.pageIndex);
    }
    
    function toPage(arr) {
        for (var i = 0; i < arr.length; i ++) {
            var results = [];
            var arrItem = arr[i];
            
            if(arrItem.type == "focus"){
                arrItem["itemRoot"] = "focus"
                arrItem["itemType"] = "focus_slide"
                arrItem["index"] = "0000";
                
                var items = arrItem.item;
                for(var j=0;j<items.length;j++){
                    var item = items[j]
                    item["screenWidth"] = com.screenWidth
                    item["navRoot"] = navRoot;
                }
                
                results.push(arrItem)
                view.insertList(results);
            
            }else if(arrItem.type == "list"){
                var results = [];
                var items = arrItem.item;
                for (var j = 0; j < items.length; j ++) {
                    var item = items[j]
                    item["itemRoot"] = "list"
                    item["itemType"] = qsTr("%1_%2%3").arg(arrItem.type).arg(item.type).arg(item.style?"_"+item.style.type:"")
                    item["index"] = com.getIndex(pageIndex, j, 4);
                    item["navRoot"] = navRoot;
                    item["screenWidth"] = com.screenWidth
                    results.push(item);
                }
                view.insertList(results)
                
                if (view.dataTotal >= arrItem.totalPage*20) {
                    view.loadFinished();
                }
            }
        }
        
        if(pageIndex==1){
            view.scrollBegin();
        }
    }
    
    onCreationCompleted: {
        navRootChanged(root)
        
        view.refreshTriggered.connect(refresh);
        view.loadmoreTriggered.connect(loadMore);
    }
    
    Page {
        actionBarAutoHideBehavior:ActionBarAutoHideBehavior.HideOnScroll
        Container {
            layout: DockLayout {
            }
            
            Loader {
                id: loader
            }
            
            PullToRefreshListView {
                id: view
                
                onTriggered: {
                    var currentItem = dataModel.data(indexPath);
                    if(currentItem.itemType == "zfooter"){
                        return;
                    }
                    
                    var linkType = currentItem.type
                    var link = currentItem.link
                    
                    if (linkType == "topic2") {
                        var docCom = Qt.createComponent("asset:///ViewTopic.qml")
                        var docView = docCom.createObject(root);
                        docView.requestUrl = currentItem.link.url;
                        root.push(docView);
                    } else if (linkType == "doc") {
                        var docCom = Qt.createComponent("asset:///ViewDoc.qml")
                        var docView = docCom.createObject(root);
                        docView.item = currentItem
                        root.push(docView);
                    } else if (linkType == "web") {
                        var docCom = Qt.createComponent("asset:///ViewWeb.qml")
                        var docView = docCom.createObject(root);
                        docView.item = currentItem
                        root.push(docView);
                    }else if(linkType == "slide"){
                        var docCom = Qt.createComponent("asset:///ViewSlider.qml")
                        var docView = docCom.createObject(root);
                        docView.url = currentItem.id;
                        docView.open();
                    }
                }
                
                listItemComponents: [
                    ListItemComponent {
                        type: "list_doc"
                        CustomListItem {
                            DocItem {
                                screenWidth: ListItemData.screenWidth
                                height: 96 * 1.5
                                width: 134 * 1.5
                                title: ListItemData.title
                                thumbnail: ListItemData.thumbnail
                                updateTime: ListItemData.updateTime
                                comments: ListItemData.comments
                            }
                        }
                    },
                    
                    ListItemComponent {
                        type: "list_slide"
                        SlideItem {
                            screenWidth: ListItemData.screenWidth
                            height: 96 * 1.5
                            width: 134 * 1.5
                            title: ListItemData.title
                            thumbnail: ListItemData.thumbnail
                            updateTime: ListItemData.updateTime
                            comments: ListItemData.comments
                        }
                    },
                    
                    ListItemComponent {
                        type: "list_slide_slides"
                        CustomListItem {
                            Slide_SlidesItem {
                                screenWidth: ListItemData.screenWidth
                                title: ListItemData.title
                                thumbnail_1: ListItemData.style.images[0]
                                thumbnail_2: ListItemData.style.images[1]
                                thumbnail_3: ListItemData.style.images[2]
                            }
                        }
                    },
                    
                    ListItemComponent {
                        type: "list_web"
                        CustomListItem {
                            preferredHeight: 96 * 1.5 + 50
                            WebItem {
                                screenWidth: ListItemData.screenWidth
                                title: ListItemData.title
                                thumbnail: ListItemData.thumbnail
                            }
                        }
                    },
                    
                    ListItemComponent {
                        type: "list_topic2"
                        CustomListItem {
                            preferredHeight: 94 * 1.5 + 10
                            Topic2 {
                                screenWidth: ListItemData.screenWidth
                                title: ListItemData.title
                                thumbnail: ListItemData.thumbnail
                            }
                        }
                    },
                    
                    ListItemComponent {
                        type: "focus_slide"
                        CustomListItem {
                            attachedObjects: [
                                Common {
                                    id: com2
                                }
                            ]
                            property int width: com2.screenWidth
                            preferredHeight: 360*(width/640)+5
                            
                            FocusItem {
                                focusItems: ListItemData.item
                            }
                        }
                    },
                    
                    ListItemComponent {
                        type: "list_text_live"
                        CustomListItem {
                            Text_LiveItem {
                                title: ListItemData.title
                            }
                        }
                    }
                ]
            }
        }
    }
    
    attachedObjects: [
        Common {
            id: com
        },
        
        DataSource {
            id: dataSource
            onSourceChanged: {
                console.log(source)
                view.startLoad();
                dataSource.load()
            }
            
            onDataLoaded: {
                toPage(data)
                view.stopLoad();
            }
            
            onError: {
                _Setting.alert(qsTr("Load Error."))
                pageIndex = parseInt(pageIndex) - 1
                view.stopLoad();
            }
        }
    ]
}

